package demo.practice.competition;

public class Fmtnum {

    public static void main(String[] args) {
        Fmtnum fmtnum= new Fmtnum();
        fmtnum.reformatNumber("1-23-45 6");
    }

    public String reformatNumber(String number) {

        StringBuffer sb=new StringBuffer();

        for(int i=0;i<number.length();i++){
            char c=number.charAt(i);
            if(c!=' '&&c!='-'){
                sb.append(number.charAt(i));
            }
        }

        String str= sb.toString();
        //3 4 ==== 3 -2- 2
        //1234
        sb=new StringBuffer();
        int len=str.length();
        if(len%3==1&& len>3){
            for(int i=0;i<len;i++){
               if((i+1)%3==0&& i<len-4){
                   sb.append(str.charAt(i));
                   sb.append("-");
               }else if(i==len-3) {
                   sb.append(str.charAt(i));
                   sb.append("-");
               }else{
                   sb.append(str.charAt(i));
               }
            }
        }else{
            for(int i=0;i<len;i++){
                if((i+1)%3==0&&i<len-1){
                    sb.append(str.charAt(i));
                    sb.append("-");
                }else{
                    sb.append(str.charAt(i));
                }
            }
        }

        return sb.toString();

    }
}
